<?php

namespace app\api\controller;

use app\common\controller\Api;
use Hedeqiang\TenIM\IM;
use think\Db;
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    private $sdkappid   = 1600023052;  #应用 sdkappid
    private $key        = '4a40884feef4c8f03a92947f7db1bf4aed04f00ab0500be3f88e014217de0e0c';  #应用秘钥
    private $identifier = 'administrator';  #应用管理员

     /**
     * 测试，别管
     */
    public function getEpisodeChapter()
    {
        if($this->auth->id){
            $user = DB::name("user")->where("id",$this->auth->id)->find();
            $lang_type = $user['lang_type'];
            if($user['lang_type'] == 1){
                $episode_script_id = $user['episode_script_id'];
            }else{
                $episode_script_id = $user['eng_episode_script_id'];
            }
        }else{
            $lang_type = 1;
            $system = DB::name("system_info")->where("type",$lang_type)->find();
            $episode_script_id = $system['episode_script_id'];
        }
        
        $episode_script = DB::name("episode_script")->where("id",$episode_script_id)->find();
        
        $where = array(
            "episode_script_id" => $episode_script_id
        );
        $result = DB::name("episode_chapter")->where($where)->order("weigh desc")->field("id,name")->select();
        // print_r($result);exit;
        foreach($result as $key => $val){
            $result[$key]['info_list'] = $this->getInfo($val['id'],'',$episode_script['video_type']);
            $result[$key]['video_type'] = $episode_script['video_type'];
            $result[$key]['branch_image'] = $episode_script['branch_image'];
            $xia_ids = DB::name("episode_info")->where("episode_chapter_id",$val['id'])->order("id asc")->field("id")->find();
            $result[$key]['xia_ids'] = $xia_ids['id'];
        }
        $this->success('查询成功', $result);
    }
    
    
    /*
    *测试，别管
    */
    public function getInfo($episode_chapter_id,$episode_info_ids,$video_type){
        
        $where = "episode_chapter_id =".$episode_chapter_id." and type = 1";
        $info = DB::name("episode_info")->where($where)->select();
        $new_info = array();
        foreach ($info as $item){
            if($this->auth->id){
                $info_episode_look = DB::name("episode_look")->where(array("user_id"=>$this->auth->id,"episode_info_id"=>$item['id']))->find();
                if($info_episode_look){
                    $item['is_look'] = 1;
                }else{
                    $item['is_look'] = 2;
                }
            }else{
                $item['is_look'] = 2;
            }
            $item['is_look'] = 1;
            // $item['xia_ids'] = '0';
            $new_info[$item['id']] = $item;
        }
        foreach ($new_info as $key => $row){
            if($row['episode_video_ids'] == 0){
                $new_info[$row['id']] = $row;
            }else{
                if(strpos($row['episode_video_ids'], ',') !== false){
                    $episode_video_ids = explode(",",$row['episode_video_ids']);
                    foreach ($episode_video_ids as $item){
                        if(isset($new_info[$item]['xia_ids'])){
                            $new_info[$item]['xia_ids'] .= ",".$row['id'];
                        }else{
                            $new_info[$item]['xia_ids'] = $row['id'];
                        }
                    }
                }else{
                    if(isset($new_info[$row['episode_video_ids']]['xia_ids'])){
                        $new_info[$row['episode_video_ids']]['xia_ids'] .= ",".$row['id'];
                    }else{
                        $new_info[$row['episode_video_ids']]['xia_ids'] = $row['id'];
                    }
                    
                }
            }
        }
        
        $info_list = array();
        foreach ($new_info as $val){
            if(!isset($val['xia_ids'])){
                $val['xia_ids'] = 0;
            }
            $info_list[] = $val;
        }
        
        return $info_list;
        
    }

    /**
     * 首页
     *
     */
    public function index()
    {
        $res = $this->set_imimport('wuliu_im1','zhangsan');
       echo "<pre>";print_r($res);exit;
        $this->add_imimport(1);
        $this->success('请求成功');
    }


    /**
     * 添加IM账号
     * @param [type] $userid [用户id]
     */
    function add_imimport($user_id){
        // require __DIR__ .'/vendor/autoload.php';
        $config = [
            'sdk_app_id' => $this->sdkappid,// 腾讯IM appid
            'identifier' => $this->identifier,
            'secret_key' => $this->key // 腾讯IM秘钥
        ];
        $im = new IM($config);
        if(!empty($info['im_account'])){
            $paramsa = [
                'CheckItem' => [
                    [
                        'UserID' => 'im_account', // 账号
                    ]
                ]
            ];
            $iminfo = $im->send('im_open_login_svc','account_check',$paramsa); // 账号是否存在
            if($iminfo['ResultItem'][0]['AccountStatus'] == 'Imported'){
                return 1;
            }
        }
    
        $account = 'wuliu_im1';
        $nick = 'user_nickname';
        $faceurl = 'https://upyun.laravelcode.cn/upload/avatar/1524205770e4fbfbff-86ae-3bf9-b7b8-e0e70ce14553.png'; // 头像
        // 创建账号
        $params = [
            'Identifier' => $account,
            'Nick'       => $nick,
            'FaceUrl'    => $faceurl,
        ];
        $result = $im->send('im_open_login_svc','account_import',$params);
        print_r($result);exit;
        return 2;
    }
    
    /**
     * [修改IM账户信息]
     * @param [type] $account [要修改的账号]
     * @param [type] $nick    [昵称]
     */
    function set_imimport($account, $nick){
        $params = [
            'From_Account' => $account,
            'ProfileItem' => [
                [
                    'Tag' => 'Tag_Profile_IM_Nick',
                    "Value" => $nick,
                ],
                [
                    'Tag' => 'Tag_Profile_IM_Image', 
                    'Value' => 'https://upyun.laravelcode.cn/upload/avatar/1524205770e4fbfbff-86ae-3bf9-b7b8-e0e70ce14553.png'
                ],
            ]
        ];
        $config = [
            'sdk_app_id' => $this->sdkappid,// 腾讯IM appid
            'identifier' => $this->identifier,
            'secret_key' => $this->key // 腾讯IM秘钥
        ];
        $im = new IM($config);
        return $im->send('profile','portrait_set',$params);
    }
}
